<?php
namespace app\common\model;


class Redis
{
    protected static $redis;
    private $index;
    private $prefix;
    public function __construct($config=[]) {
        if(empty($config)){
            $config=config('redis');
        }
        if(empty($config['host'])){
            throw new \think\exception\HttpException(200,'redis主机参数错误');
        }
        if(empty($config['port'])){
            $config['port']=6379;
        }
        if(isset($config['prefix'])){
            $this->prefix=$config['prefix'];
        }else{
            $this->prefix='';
        }
        $index=$config['host'].'.'.$config['port'];
        if(isset(self::$redis[$index])){
            $this->index=$index;
        }else{
            $r=new \Redis();
            $conn=$r->connect($config['host'], $config['port']);
            if(!$conn){
                throw new \think\exception\HttpException(200,'redis连接失败');
            }else{
                self::$redis[$index]=$r;
                $this->index=$index;
            }
        }
    }


    public function get($key){
        $key=$this->prefix.$key;
        return self::$redis[$this->index]->get($key);
    }
    public function keys($key){
        $key=$this->prefix.$key;
        return self::$redis[$this->index]->keys($key);
    }
    public function set($key,$value,$ttl=0){
        $key=$this->prefix.$key;
        return self::$redis[$this->index]->set($key,$value,$ttl);
    }
    public function hGetAll($key){
        $key=$this->prefix.$key;
        return self::$redis[$this->index]->hGetAll($key);
    }
    public function expire($key,$ttl){
        $key=$this->prefix.$key;
        return self::$redis[$this->index]->expire($key,$ttl);
    }
    public function hSet($key,$hashKey,$value,$ttl=0){
        $key=$this->prefix.$key;
        $rs=self::$redis[$this->index]->hSet($key,$hashKey,$value);
        if($ttl>0){
            $this->expire($key,$ttl);
        }
        return $rs;
    }
    public function hMset($key,$hashKeys,$ttl=0){
        $key=$this->prefix.$key;
        $rs=self::$redis[$this->index]->hMset($key,$hashKeys);
        if($ttl>0){
            $this->expire($key,$ttl);
        }
        return $rs;
    }
    public function hGet($key,$hashKey){
        $key=$this->prefix.$key;
        return self::$redis[$this->index]->hGet($key,$hashKey);
    }
    public function hMget($key,$hashKeys){
        $key=$this->prefix.$key;
        return self::$redis[$this->index]->hMget($key,$hashKeys);
    }
    public function exists($key){
        $key=$this->prefix.$key;
        return self::$redis[$this->index]->exists($key);
    }
    public function lPush($key,$value){
        $key=$this->prefix.$key;
        return self::$redis[$this->index]->lPush($key,$value);
    }
    public function brPop($key,$ttl=0){
        $key=$this->prefix.$key;
        return self::$redis[$this->index]->brPop($key,$ttl);
    }
    public function incr($key){
        $key=$this->prefix.$key;
        return self::$redis[$this->index]->incr($key);
    }
    public function delete($key){
        $key=$this->prefix.$key;
        self::$redis[$this->index]->delete($key);
    }
    public function hIncrBy($key,$hashKey,$value){
        $key=$this->prefix.$key;
        return self::$redis[$this->index]->hIncrBy($key,$hashKey,$value);
    }
    public function rename($sKey,$dKey){
        $srcKey=$this->prefix.$sKey;
        $dstKey=$this->prefix.$dKey;
        return self::$redis[$this->index]->rename($srcKey,$dstKey);
    }
    public function sAdd( $key, $value1, $value2 = null, $valueN = null ){
        if(!is_array($key)){
            $args=func_get_args();
        }else{
            $args=$key;
        }

        $key=$this->prefix.$args[0];
        unset($args[0]);
        foreach($args as $v){
            self::$redis[$this->index]->sAdd($key,$v);
        }
    }
    public function sUnion( $key1, $key2=null, $keyN = null ){
        if(!is_array($key1)){
            $args=func_get_args();
        }else{
            $args=$key1;
        }

        $params=[];
        $i=1;
        foreach($args as $v){
            $params['key'.$i++]=$this->prefix.$v;
        }
        return self::$redis[$this->index]->sUnion($params);
    }

    public function multi(){
        self::$redis[$this->index]->multi();
    }
    public function exec(){
        self::$redis[$this->index]->exec();
    }
}